﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockManagement.Model;
using StockManagement.Helper;
using StockManagement.Commands;
using System.Collections.ObjectModel;
using StockManagement.Views;

namespace StockManagement.ViewModels
{
    public class InwardMateriallViewModel:BaseViewModel
    {
        MaterialViewModel _materialVM;
        VendorViewModel _vendorVM;
        string _materialCode;
        EnumMaterialType _materialType;
        //string _vendor;
        double _rate;
        double _VAT;
        int _quantity;
        bool _IsMaterialCodeValid;
        bool _bEditMode = true;

        DelegateCommand _updateEditCommand;
        DelegateCommand _discardCommand;
        DelegateCommand _deleteCommand;
        DelegateCommand _addVendorCommand;
        
        public event MaterialDeletedDelegate MaterialDeleted;
        public event MaterialUpdatedDelegate MaterialUpdated;

        ObservableCollection<MaterialViewModel> _MaterialCollection;
        ObservableCollection<VendorViewModel> _vendorCollection;

        public InwardMateriallViewModel(ObservableCollection<MaterialViewModel> materialCollection,ObservableCollection<VendorViewModel> vendorCollection)
        {
            PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(EditMaterialViewModel_PropertyChanged);
            _MaterialCollection = materialCollection;
            _vendorCollection = vendorCollection;
            _MaterialCollection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(materialCollection_CollectionChanged);
            _updateEditCommand = new DelegateCommand(ExecuteUpdateMaterialCommand, CanExecuteCommand);
            _deleteCommand = new DelegateCommand(ExecuteDeleteCommand, CanExecuteDeleteCommand);
            _discardCommand = new DelegateCommand(ExecuteDiscardCommand, CanExecuteCommand);
            _addVendorCommand = new DelegateCommand(ExecuteAddVendorCommand);
            SelectedMaterial = MaterialCollection.FirstOrDefault();
        }

        void EditMaterialViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "SelectedMaterial":
                    InitializeProperties();
                    break;
                default:
                    break;
            }
            RaiseCanExecuteCommands();
        }

        void materialCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {

        }

        private void InitializeProperties()
        {
            bool bNotNull = (_materialVM != null);
            _bEditMode = false;
            MaterialCode = bNotNull ? _materialVM.MaterialCode : string.Empty;
            MaterialType = bNotNull ? _materialVM.MaterialType : EnumMaterialType.Trading;            
            SelectedVendor = bNotNull ? _vendorCollection.FirstOrDefault(p=>p.VendorID == _materialVM.VendorID): null;
            MaterialRate = bNotNull ? _materialVM.MaterialRate : 0;
            MaterialVAT = bNotNull ? _materialVM.MaterialVAT : 0;
            Quantity = bNotNull ? _materialVM.Quantity : 0;
            _bEditMode = true;
        }

        public string MaterialCode
        {
            get { return _materialCode; }
            set
            {
                _materialCode = value;
                OnPropertyChanged("MaterialCode");
            }
        }

        public EnumMaterialType MaterialType
        {
            get { return _materialType; }
            set
            {
                _materialType = value;
                OnPropertyChanged("MaterialType");
            }
        }

        //public Guid Vendor
        //{
        //    get { return _vendor; }
        //    set
        //    {
        //        _vendor = value;
        //        OnPropertyChanged("Vendor");
        //    }
        //}

        public double MaterialRate
        {
            get { return _rate; }
            set
            {
                _rate = value;
                OnPropertyChanged("MaterialRate");
            }
        }

        public double MaterialVAT
        {
            get { return _VAT; }
            set
            {
                _VAT = value;
                OnPropertyChanged("MaterialVAT");
            }
        }

        public int Quantity
        {
            get { return _quantity; }
            set
            {
                _quantity = value;
                OnPropertyChanged("Quantity");
            }
        }
        
        public bool IsMaterialCodeValid
        {
            get { return _IsMaterialCodeValid; }
            set
            {
                _IsMaterialCodeValid = value;
                OnPropertyChanged("IsMaterialCodeValid");
            }
        }

        public DelegateCommand UpdateMaterialCommand
        {
            get
            {
                return _updateEditCommand;
            }
        }

        public DelegateCommand DiscardCommand
        {
            get { return _discardCommand; }
        }

        public DelegateCommand DeleteCommand
        {
            get { return _deleteCommand; }
        }

        public DelegateCommand AddVendorCommand
        {
            get { return _addVendorCommand; }
        }


        private bool CanExecuteCommand(object parameter)
        {
            if (_materialVM == null)
                return false;

            if (MaterialCode.Trim() != _materialVM.MaterialName||
                MaterialType != _materialVM.MaterialType ||
                SelectedVendor.VendorID != _materialVM.VendorID ||
                //Vendor.Trim() != _materialVM.Vendor || 
                MaterialRate != _materialVM.MaterialRate ||
                MaterialVAT != _materialVM.MaterialVAT ||
                Quantity != _materialVM.Quantity)
                return true;
            return false;
        }

        private bool CanExecuteDeleteCommand(object parameter)
        {
            return (_materialVM != null);
        }

        private void ExecuteUpdateMaterialCommand(object parameter)
        {
            string strmessage = string.Format("Do you want to update the material '{0}'?", SelectedMaterial.MaterialName);
            var result = ShowConfirmationMessageBox(strmessage, "Update Material");
            if (!result)
                return;

            if (!_materialVM.UpdateMaterial(UpdateType.Edit, DateTime.Now, SelectedVendor.VendorID,MaterialCode, MaterialRate, Quantity, MaterialVAT))
                return;

            UpdateMaterialProperties();
        }

        private void UpdateMaterialProperties()
        {
            _materialVM.MaterialCode = _materialCode;
            _materialVM.VendorID = SelectedVendor.VendorID;
            _materialVM.MaterialRate = _rate;
            _materialVM.Quantity = _quantity;
            _materialVM.MaterialVAT = MaterialVAT;
            _materialVM.Date = DateTime.Now;

            if (MaterialUpdated != null)
                MaterialUpdated();
            
        }

        private void ExecuteDiscardCommand(object parameter)
        {
            string strmessage = string.Format("Do you want to discard the changes to  material '{0}'?", SelectedMaterial.MaterialName);
            var result = ShowConfirmationMessageBox(strmessage, "Discard Changes");
            if (!result)
                return;

            InitializeProperties();
            RaiseCanExecuteCommands();
        }

        private void ExecuteAddVendorCommand(object parameter)
        {
            NewVendor vendorview = new NewVendor();
            var bresult = vendorview.ShowDialog();
            if ((bool)bresult)
            {                
                _vendorCollection.Add(vendorview.ViewModel);
            }
        }

        //private bool FilterPredicate(object value)
        //{
        //    // We don't like nulls.
        //    if (value == null)
        //        return false;

        //    // If there is no text, there's no reason to filter.
        //    if (this.Text.Length == 0)
        //        return true;

        //    string prefix = this.Text;

        //    // If the end of the text is selected, do not mind it.
        //    if (this.length > 0 && this.start + this.length == this.Text.Length)
        //    {
        //        prefix = prefix.Substring(0, this.start);
        //    }

        //    return value.ToString()
        //        .StartsWith(prefix, !this.IsCaseSensitive, CultureInfo.CurrentCulture);
        //}

        private void RaiseCanExecuteCommands()
        {
            UpdateMaterialCommand.RaiseCanExecuteChanged();
            DiscardCommand.RaiseCanExecuteChanged();
            DeleteCommand.RaiseCanExecuteChanged();
        }

        private void ExecuteDeleteCommand(object item)
        {
            string strmessage = string.Format("Do you want to delete the material '{0}'?", SelectedMaterial.MaterialName);
            var result = ShowConfirmationMessageBox(strmessage, "Delete Material");
            if (!result)
                return;

            if (_materialVM.DeleteMaterial() && MaterialDeleted != null)
                MaterialDeleted(_materialVM.MaterialID);
            RaiseCanExecuteCommands();
        }

        public ObservableCollection<MaterialViewModel> MaterialCollection
        {
            get
            {
                return _MaterialCollection;
            }
        }

        public ObservableCollection<VendorViewModel> VendorCollection
        {
            get
            {
                return _vendorCollection;
            }
        }

        public MaterialViewModel SelectedMaterial
        {
            get
            {
                if (_materialVM == null)
                    _materialVM = _MaterialCollection.FirstOrDefault();
                return _materialVM;
            }
            set
            {
                _materialVM = value;
                OnPropertyChanged("SelectedMaterial");
            }
        }

        public VendorViewModel SelectedVendor
        {
            get
            {
                if (_vendorVM == null)
                    _vendorVM = _vendorCollection.FirstOrDefault();
                return _vendorVM;
            }
            set
            {
                _vendorVM = value;
                OnPropertyChanged("SelectedVendor");
            }
        }
    }
}
